package logic

import (
	"comm-sys/models/mysql"
	"comm-sys/userrpc/internal/svc"
	"comm-sys/userrpc/userrpc"
	"context"
	"errors"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsAddLogic {
	return &GoodsAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 商品创建
func (l *GoodsAddLogic) GoodsAdd(in *userrpc.GoodsAddRequest) (*userrpc.GoodsAddResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User
	if err := user.UserShow(mysql.DB, in.UserId); err != nil {
		return nil, errors.New("用户不存在,当前未登录")
	}

	goods := mysql.Goods{
		GoodsName:  in.GoodsName,
		GoodsPrice: in.GoodsPrice,
		GoodsCode:  in.GoodsCode,
		People:     in.People,
		UserId:     in.UserId,
	}
	if err := goods.GoodsAdd(mysql.DB); err != nil {
		return nil, errors.New("商品添加失败")
	}
	return &userrpc.GoodsAddResponse{
		GoodsId: int64(goods.ID),
	}, nil
}
